package ipl.projet.domaine;

import ipl.projet.exceptions.CoordonneException;

import java.io.Serializable;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "VAISSEAU", schema = "STARTREK")
public class Vaisseau implements Serializable {

	private static final long serialVersionUID = -6242856491244867728L;

	public enum Vaisseaux {
		RAVEN("Raven", 2), NX_CLASS("NX class", 3), SARAJEVO("Sarajevo", 3), STEAMRUNNER(
				"Steam Runner", 4), SOVEREIGN("Sovereign", 5);

		private final String name;
		private final int size;

		Vaisseaux(String name, int size) {
			this.name = name;
			this.size = size;
		}

		public String shipName() {
			return name;
		}
		
		// Pour pouvoir l'utiliser en jsp
		public String getShipName() {
			return name;
		}

		public int size() {
			return size;
		}
	}

	@Id
	@GeneratedValue
	@Column(name = "ID")
	private int id;

	@Embedded
	@AttributeOverrides({
			@AttributeOverride(name = "x", column = @Column(name = "x_DEB")),
			@AttributeOverride(name = "y", column = @Column(name = "y_DEB")) })
	private Coordonnee coordonneeDebut;

	@Embedded
	@AttributeOverrides({
			@AttributeOverride(name = "x", column = @Column(name = "x_FIN")),
			@AttributeOverride(name = "y", column = @Column(name = "y_FIN")) })
	private Coordonnee coordonneeFIN;

	@Column(name = "DEGATS")
	private int degats;

	@Column(name = "DESINTEGRE")
	private boolean desintegre;

	@Column(name = "type")
	private Vaisseaux type;

	@Column(name = "enPlace")
	private boolean enPlace;

	/*	Constructeurs	*/
	
	public Vaisseau() {

	}

	public Vaisseau(Vaisseaux type) {
		this.type = type;
		this.degats = 0;
		this.desintegre = false;
		enPlace = false;
		Coordonnee c = null;
		try {
			 c = new Coordonnee(0,0);
		} catch (CoordonneException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		coordonneeDebut = c;
		coordonneeFIN = c;
	}

	/*	Getters & Setters	*/

	public int getId() {
		return this.id;
	}

	public boolean estPlace() {
		return enPlace;
	}
	
	// Pour pouvoir l'utiliser en jsp
	public boolean getPlace() {
		return enPlace;
	}

	public boolean estTouche(Tir t) {

		int tirX = t.getCoordonneeTir().getX();
		int tirY = t.getCoordonneeTir().getY();

		int vStartX = coordonneeDebut.getX();
		int vStartY = coordonneeDebut.getY();

		int vEndX = coordonneeFIN.getX();
		int vEndY = coordonneeFIN.getY();

		// cas d'un vaisseau vertical
		if (vStartX - vEndX == 0) {
			if (tirX == vStartX) {
				if (tirY >= vStartY && tirY <= vEndY)
					return true;
			}
		}
		// cas d'un vaisseau horizontal
		if (vStartY - vEndY == 0) {
			if (tirY == vStartY) {
				if (tirX >= vStartX && tirX <= vEndX)
					return true;
			}
		}

		return false;
	}

	public Vaisseau recevoirTir(Tir tir) {

		if (estTouche(tir)) {
			this.degats++;
			if (this.degats >= type.size())
				desintegre = true;
			return this;
		}

		return null;

	}

	public boolean estDesintegre() {
		return this.desintegre;
	}

	public int getDegats() {
		return this.degats;
	}

	public int getTaille() {
		return this.type.size();
	}

	public Coordonnee getCoordonneeDebut() {
		return this.coordonneeDebut;
	}

	public void setCoordonneeDebut(Coordonnee coordonneeDebut) {
		this.coordonneeDebut = coordonneeDebut;
	}

	public Coordonnee getCoordonneeFIN() {
		return this.coordonneeFIN;
	}

	public void setCoordonneeFIN(Coordonnee coordonneeFIN) {
		this.coordonneeFIN = coordonneeFIN;
	}

	public Vaisseaux getType() {
		return type;
	}
	
	public void place(){
		this.enPlace = true;
	}
	
	public boolean estHorizontal(){
		return !estVertical();
	}
	
	public boolean estVertical(){
		return coordonneeDebut.getX()==coordonneeFIN.getX();
	}

}
